package com.fly.core.utils.persistence;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

/**
 * 动态查询对象
 * 
 * @author <a href="mailto:ketayao@gmail.com">ketayao</a>
 * @since 2018-09-11 上午11:30:53
 */
public class SearchFilter
{
    public enum Operator
    {
        EQ, LIKE, GT, LT, GTE, LTE, IN, ISNULL, ISNOTNULL
    }
    
    private String fieldName;
    
    private Object value;
    
    private Operator operator;
    
    public SearchFilter(String fieldName, Operator operator, Object value)
    {
        this.fieldName = fieldName;
        this.value = value;
        this.operator = operator;
    }
    
    /**
     * searchParams中key的格式为OPERATOR_FIELDNAME
     */
    public static Map<String, SearchFilter> parse(Map<String, Object> searchParams)
    {
        Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
        for (Entry<String, Object> entry : searchParams.entrySet())
        {
            // 过滤掉空值
            String key = entry.getKey();
            Object value = entry.getValue();
            if (StringUtils.isBlank((String)value))
            {
                continue;
            }
            // 拆分operator与filedAttribute
            String[] names = StringUtils.split(key, "_");
            if (names.length != 2)
            {
                throw new IllegalArgumentException(key + " is not a valid search filter name");
            }
            String filedName = names[1];
            Operator operator = Operator.valueOf(names[0]);
            // 创建searchFilter
            SearchFilter filter = new SearchFilter(filedName, operator, value);
            filters.put(key, filter);
        }
        return filters;
    }
    
    /**
     * @return the fieldName
     */
    public String getFieldName()
    {
        return fieldName;
    }
    
    /**
     * @param fieldName the fieldName to set
     */
    public void setFieldName(String fieldName)
    {
        this.fieldName = fieldName;
    }
    
    /**
     * @return the value
     */
    public Object getValue()
    {
        return value;
    }
    
    /**
     * @param value the value to set
     */
    public void setValue(Object value)
    {
        this.value = value;
    }
    
    /**
     * @return the operator
     */
    public Operator getOperator()
    {
        return operator;
    }
    
    /**
     * @param operator the operator to set
     */
    public void setOperator(Operator operator)
    {
        this.operator = operator;
    }
}